let flag = true;

let loading;
$(function () {
    const token = window.localStorage.getItem("Authorization");
    if (null == token) {
        window.location.href = ("/index/none/toLogin");
    }
    // 设置jQuery Ajax全局的参数
    $.ajaxSetup({
        headers: {
            "Authorization": window.localStorage.getItem("Authorization")
        },
        beforeSend: function () {
            loading = load();
        },
        // error:function (){
        //   failure("请求失败");
        // },
        complete: function (XMLHttpRequest, textStatus) {
            let code = XMLHttpRequest.status;
            let res = XMLHttpRequest.responseText;
            let data = JSON.parse(res);

            switch (data.status) {
                case 200:
                    layer.close(loading);
                    break;
                case 204:
                    layer.close(loading);
                    failure(data.msg);
                    break;
                case 401:
                    alert("身份信息过期");
                    window.location.href = ("/index/none/toLogin");
                    break
                // case 403:
                //     failure("权限不足");
                //     break;
                default:
                    layer.close(loading);
                    failure(data.msg);
            }
            flag = true;
            // switch (code) {
            //     case 403:
            //         failure("权限不足");
            //         break;
            //     case 500:
            //         failure("操作失败");
            //         break;
            // }
        }
    });
    $('.ui.dropdown').dropdown({
        on: 'click'
    });

    $('a.left-item').click(function () {
        $("a.left-item").removeClass("active");
        $(this).addClass("active");
    });
    $('a.head-item').click(function () {
        $("a.head-item").removeClass("active");
        $(this).addClass("active");
    });
    //esc关闭模态框
    $(document).keydown(function (event) {
        if (event.which === 27) {
            $(".ui.modal").modal("hide");
        }
    });
    //跳转到工艺页面
    $(document).on("click", '#craft', function () {
        $.get({
            url: '/index/check/checkCraft',
            success: function (result) {
                if (result.status === 200) {
                    window.location.href = ("/index/page/craft");
                }
            }
        })
    });
    //跳转到管理员界面
    $(document).on("click", '#admin', function () {
        $.get({
            url: '/index/check/checkAdmin',
            success: function (result) {
                if (result.status === 200) {
                    window.location.href = ("/index/page/admin");
                }
            }
        })
    });

    //跳转到工时界面
    $(document).on("click", '#hours', function () {
        $.get({
            url: '/index/check/checkHours',
            success: function (result) {
                if (result.status === 200) {
                    window.location.href = ("/index/page/hours");
                }
            }
        })
    });
})


